//
//  Explore.m
//  XPhoto
//
//  Created by baboy on 2019/8/26.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "Explore.h"
#import <BKKit/BKApi.h>
#import "HttpResponse.h"

#define EXPLORE_SEARCH_PAGE_SIZE 30

@implementation Explore

+ (id)searchPostsWithArgs:(ExploreSearchArgs *)args
                 callback:(void(^)(id task, id response, NSError *error))callback{
    if (args.pageSize <= 0) {
        args.pageSize = EXPLORE_SEARCH_PAGE_SIZE;
    }
    id task = [BKApi request:API_EXPLORE_SEARCH_POSTS
                  parameters:args
                    callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
        id arr = nil;
        if (!error) {
            HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[Post class]];
            if ([response isSuccess]) {
                arr = response.data;
            }
            if(![response isSuccess]){
                error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
            }
        }
        if (callback) {
            callback(task,arr, error);
        }
    }];
    return task;
}

+ (id)searchWithArgs:(ExploreSearchArgs *)args
            callback:(void(^)(id task, id response, NSError *error))callback{
    
    id task = [BKApi request:API_EXPLORE_SEARCH
                  parameters:args
                    callback:^(id  _Nonnull task, id  _Nullable json, NSError * _Nullable error) {
        NSMutableArray *arr = nil;
        if (!error) {
            HttpResponse *response = [HttpResponse responseWithDictionary:json dataItemClass:[ExploreItem class]];
            if ([response isSuccess]) {
                arr = response.data;
                arr = [NSMutableArray array];
                for (NSInteger i = 0, n = [response.data count]; i < n; i++) {
                    ExploreItem *item = [response.data objectAtIndex:i];
                    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_USER]) {
                        User *user = [[User alloc] initWithDictionary:item.data];
                        item.data = user;
                        [arr addObject:item];
                    }
                    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_LOC]) {
                        PostLoc *loc = [[PostLoc alloc] initWithDictionary:item.data];
                        item.data = loc;
                        [arr addObject:item];
                        
                    }
                    if ([item.type isEqualToString:EXPLORE_SEARCH_TYPE_TOPIC]) {
                        PostTopic *topic = [[PostTopic alloc] initWithDictionary:item.data];
                        item.data = topic;
                        [arr addObject:item];
                        
                    }
                }
            }
            if(![response isSuccess]){
                error = [NSError errorWithDomain:HttpRequestDomain code:response.status userInfo:@{NSLocalizedDescriptionKey:response.msg}];
            }
        }
        if (callback) {
            callback(task,arr, error);
        }
    }];
    return task;
}
@end
@implementation ExploreItem
@end
@implementation ExploreSearchArgs
@end
